package nettyhttpserver;
import java.util.Properties;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
public class HttpServer {
	public static void start(int port) throws Exception {
		ServerBootstrap b = new ServerBootstrap();
		NioEventLoopGroup group = new NioEventLoopGroup();
		b.group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
			@Override
			public void initChannel(SocketChannel ch) throws Exception {
				System.out.println("initChannel ch:" + ch);
				ch.pipeline().addLast("decoder", new HttpRequestDecoder()) // 1
						.addLast("aggregator", new HttpObjectAggregator(9000 * 1024)) // 3
						.addLast("encoder", new HttpResponseEncoder()) // 2
						.addLast("handler3", new HttpTestHandler());
			}
		}).option(ChannelOption.SO_BACKLOG, 128) // determining the number of connections queued
				.childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
		ChannelFuture future = b.bind("0.0.0.0", port).sync();
		System.out.println("HTTP Server startup.");
		future.channel().closeFuture().sync();
	}
}
